/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * Project Name: lmrp-android app
 * Create Time: 16-2-16 下午6:43
 */

package com.lenovo.mso2o.ui.adapter;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.lenovo.framework.util.maputil.MapDistance;
import com.lenovo.mso2o.R;

import java.util.ArrayList;
import java.util.List;

public class PoiAdapter extends RecyclerView.Adapter<PoiAdapter.ViewHolder>  {
    private List<PoiInfo> poiInfoList;
    private LatLng latLng;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public PoiAdapter(List<PoiInfo> items) {
        if(items == null)
            this.poiInfoList = new ArrayList<>();
        else
            this.poiInfoList = items;
    }

    public void setList(List<PoiInfo> list) {
        this.poiInfoList = list;
        notifyDataSetChanged();
    }

    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(int position, String address, LatLng location);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.poi_item, parent, false);
        return new ViewHolder(v);
    }

    @Override public void onBindViewHolder(ViewHolder holder, int position) {
        PoiInfo item = poiInfoList.get(position);
        holder.shopDetail.setText(item.name);
        holder.district.setText(item.address);
        if(latLng != null) {
            //double lon1, double lat1, double lon2, double lat2
            String dis = MapDistance.getInstance().getShortDistance(latLng.longitude, latLng.latitude, item.location.longitude, item.location.latitude);
            holder.distance.setText(dis);
        }else{
            holder.distance.setText("");
        }
    }

    @Override public int getItemCount() {
        return poiInfoList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView shopDetail;
        public TextView district;
        public TextView distance;

        public ViewHolder(final View rootView) {
            super(rootView);
            shopDetail = (TextView) rootView.findViewById(R.id.tv_addr_shop_detail);
            district = (TextView) rootView.findViewById(R.id.tv_addr_district);
            distance = (TextView) rootView.findViewById(R.id.tv_distance_item_search_map);
            rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = getLayoutPosition();
                    if (mOnItemClickListener != null && pos != -1) {
                        mOnItemClickListener.onItemClick(pos, poiInfoList.get(pos).name + " " + poiInfoList.get(pos).address , poiInfoList.get(pos).location);
                    }
                }
            });
        }
    }

    public void setLatLng(LatLng latLng){
        this.latLng = latLng;
    }
}